package com.example.dto;

import com.example.model.dto.BaseDTO;
import com.example.validator.blankOrEmptyAllowed.BlankAndEmpty;
import com.example.validator.group.AddGroup;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Null;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * OAuth2客户端应用DTO
 */
@Data
@Schema(description = "OAuth2客户端应用DTO")
@JsonIgnoreProperties(ignoreUnknown = true) // 标注 UserDetail 类以忽略未知属性
@NoArgsConstructor
public class Oauth2RegisteredClientDTO extends BaseDTO implements Serializable {
    private static final long serialVersionUID = 1L;

    @BlankAndEmpty(message = "{id.null}", groups = AddGroup.class)
    @NotNull(message = "{id.require}", groups = UpdateGroup.class)
    @Schema(description = "客户端唯一标识主键", required = false)
    private String id;

    @Schema(description = "客户端ID", required = true, example = "password-client")
//    @NotBlank(message = "{client.id.require}", groups = DefaultGroup.class)
    private String clientId;

    @Schema(description = "客户端密钥", required = true, example = "password-client-secret")
    private String clientSecret;

    @Schema(description = "客户端ID颁发时间")
    private Date clientIdIssuedAt;
    @Schema(description = "客户端密钥过期时间")
    private Date clientSecretExpiresAt;


    @Schema(description = "客户端名称", required = true, example = "超级客户端")
    @NotBlank(message = "{client.name.require}", groups = DefaultGroup.class)
    private String clientName;
    private String clientSecretPlaintext;

    @Schema(description = "客户端认证方式（JSON数组）", required = true, example = "[\"client_secret_basic\"]")
    @JsonSerialize(using = ClientAuthenticationMethodSerializer.class)
    @JsonDeserialize(using = ClientAuthenticationMethodDeserializer.class)
    private Set<ClientAuthenticationMethod> clientAuthenticationMethods;

    @Schema(description = "授权类型", required = true, example = "[\"refresh_token\",\"password\",\"client_credentials\",\"authorization_code\"]")
    @JsonSerialize(using = AuthorizationGrantTypeSerializer.class)
    @JsonDeserialize(using = AuthorizationGrantTypeDeserializer.class)
    private Set<AuthorizationGrantType> authorizationGrantTypes;

    @Schema(description = "授权回调URI（JSON数组）", required = true, example = "https://www.baidu.com")
    private String redirectUris;

    @Schema(description = "退出登录后回调URI（JSON数组）")
    private String postLogoutRedirectUris;

    @Schema(description = "授权范围（JSON数组）", required = true, example = "[\"openid\",\"profile\", \"message.read\"]")
    @NotNull(message="{scopes.require}", groups = DefaultGroup.class)
    private Set<String> scopes;

    @Schema(description = "是否需要授权", required = true)
    private ClientSettingsDTO clientSettingsDTO;

    @Schema(description = "令牌配置（JSON字符串）")
    @NotNull(message="{token.settings.require}", groups = DefaultGroup.class)
    private TokenSettingsDTO tokenSettings;

    @Schema(description = "门店ID", required = false, example = "0")
    private Long storeId;

    @Schema(description = "是否是业务端", required = false, example = "0")
    private Integer isBusinessUser;
}